package com.situ.mall.admin.controller;
import com.situ.mall.admin.entity.Attachment;
import com.situ.mall.admin.service.IAttachmentService;
import com.situ.mall.admin.util.StringUtil;
import com.situ.mall.common.vo.ResultVO;
import com.situ.mall.common.entity.Product;
import com.situ.mall.common.service.IProductService;
import com.situ.mall.common.vo.LayuiTableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author membergpf
 * @since 2023-03-18
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    IProductService productService;
    @Autowired
    IAttachmentService attachmentService;


    @GetMapping("/detail")
    public String detail(){
        return "";
    }



    @GetMapping("/page")
    public String page() {
        return "product";
    }

    @GetMapping("/listAll")
    @ResponseBody
    public LayuiTableVO list(String name, String version, Integer page, Integer limit) {
        return productService.listAll(name, version, page, limit);
    }

    @PostMapping("/del")
    @ResponseBody
    public void deleteProduct(Integer id) {
        productService.updateByIdL(id);
    }

    @PostMapping("/save")
    @ResponseBody
    public ResultVO save(Product product, String imgid) {
        if (product.getId() == null) {
            productService.save(product);
            Long maxId = productService.maxId();
            int sequence = 0;
            for (String id : imgid.split(",")) {
                //将前端传来的多张图片的id分隔开
                //将商品的最大ID放入图片id和分隔开的id相等的商品ID里
                sequence++;
                attachmentService.update1(Integer.parseInt(id), maxId.intValue(), product.getName(),sequence);

            }
        } else {
            Product oldProduct = new Product();
//          oldProduct = productService.listById(product.getProId());
            productService.saveOrUpdate(product);
        }
        ResultVO vo = new ResultVO();
        vo.setMsg("保存成功");
        return vo;
    }

    @PostMapping("/removeByIds")
    @ResponseBody
    public void selectById(@RequestParam("ids[]") Integer[] ids) {
        productService.updateMore(ids);
    }

    @PostMapping("/homeImg")
    @ResponseBody
    public ResultVO avatar(MultipartFile file) {
        ResultVO vo = new ResultVO();
        vo.setMsg("上传成功");
        String fileName = System.currentTimeMillis() + ".png";
        File f = new File("D:\\test\\" + fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
//        System.out.println("接收到请求");
        String src ="/upload/"+fileName;
        Map<String, String> map = new HashMap<>();
        map.put("src", src);
        vo.setData(map);
//        System.out.println(map);
        return vo;
    }

    @PostMapping("/attach")
    @ResponseBody
    public synchronized ResultVO upload(MultipartFile file) {
//以下将图片起别名后放入本地文件夹
        String fileName = System.currentTimeMillis() + ".png";
        File f = new File("D:\\test\\" + fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //可以依据upload来查询上传到本地的图片
        String src ="/upload/"+fileName;

        //创建图片service保存图片本地地址进数据库

        //创建img对象来存储图片的本地地址
        Attachment attachment = new Attachment();
        attachment.setPath(src);
        attachment.setFileName(file.getOriginalFilename());
        //使用String工具类吧long类型的文件大小转化字符串类型的
        attachment.setFileSize(StringUtil.formatFileSize(file.getSize()));
        attachment.setType(file.getContentType());
        attachment.setCreatetime(LocalDateTime.now());



        //放入数据库
        //返回的是一个图片数据库中id的最大值
        Integer id = attachmentService.insert(attachment);

        //使用要求的格式给前端返回信息
        ResultVO vo = new ResultVO();
        vo.setCode(0);
        vo.setMsg("上传成功");
        Map<String, Integer> map = new HashMap<>();
        //将最大值放入data传入前端
        map.put("id", id);
        vo.setData(map);
        return vo;
    }

    @PostMapping("/edit")
    @ResponseBody
    public ResultVO edit(Integer id) {
        return productService.listById(id);
    }

    @PostMapping("/up")
    @ResponseBody
    public void up(@RequestParam("ids[]") Integer[] ids) {
        productService.updateUp(ids);
    }

    @PostMapping("/down")
    @ResponseBody
    public void down(@RequestParam("ids[]") Integer[] ids) {
        productService.updateDown(ids);
    }

}
